package goos

import (
	"errors"
	"fmt"
	"os"
	"path/filepath"
	"runtime"
)

type swooleBin string

const (
	LINUX_PATH  swooleBin = "swoole-cli"
	DARWIN_PATH swooleBin = "swoole-cli"
	//WINDOWS_PATH swooleBin = "swoole-cli.exe"
	WINDOWS_PATH swooleBin = "windows64\\bin\\swoole-cli.exe"
	//WINDOWS_PATH swooleBin = "C:\\swoole\\bin\\swoole-cli.exe"
)

var GoosMap = map[string]swooleBin{
	`linux`:   LINUX_PATH,
	`windows`: WINDOWS_PATH,
	`darwin`:  DARWIN_PATH,
}

type GoodsBin struct {
}

//获取默认得bin文件
func GetDefaultGoodsBin() *GoodsBin {
	return new(GoodsBin)
}

func (goodsBin *GoodsBin) GetCliBin() (string, error) {

	if 32<<(^uint(0)>>63) == 32 {
		return "", errors.New("不支持32位系统!")
	}

	execpath, _ := os.Executable() // 获得程序路径
	if bin, ok := GoosMap[runtime.GOOS]; ok {
		binPath := filepath.Join(filepath.Dir(execpath) + string(os.PathSeparator) + string(bin))
		fmt.Println(binPath)
		//binPath := string(bin)
		_, err := os.Stat(binPath)
		if err == nil {
			return binPath, nil
		}
		if os.IsNotExist(err) {
			return "", errors.New("文件或文件夹不存在!")
		}
		return "", err
	}

	return "", errors.New("暂不支持该系统!")
}
